package tools;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class socketServer {
    public static void main(String[] args) throws IOException {
        //1.创建一个服务器端Socket，即ServerSocket，指定绑定的端口，并监听此端口
        ServerSocket serverSocket = new ServerSocket(12345);
        while(true) {
            Socket socket = null;
            //2.调用accept()等待客户端连接
            System.out.println("~~~服务端已就绪，等待客户端接入~ ");
            socket = serverSocket.accept();
            //3.连接后获取输入流，读取客户端信息
            InputStream is = null;
            InputStreamReader isr = null;
            BufferedReader br = null;
            is = socket.getInputStream();     //获取输入流
            isr = new InputStreamReader(is, "UTF-8");
            br = new BufferedReader(isr);
            String info = null;
            String cbf_request = "";
            while ((info = br.readLine()) != null) {//循环读取客户端的信息
                cbf_request = cbf_request + info;
            }
            System.out.println("客户端发送过来的信息" + cbf_request);
            socket.shutdownInput();//关闭输入流

            OutputStream os = socket.getOutputStream();//字节输出流
            PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流
            String cbf_response="";

            cbf_response=new Transfer().transfer(cbf_request);

            pw.write(cbf_response);
            pw.flush();
            socket.shutdownOutput();//关闭输出流

            socket.close();
        }
    }
}